<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class getBugIfo extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('getBugIfo')
            ->setDescription('获取虫情数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $url = "http://insect.sennor.net:1881/getInsectNewCollectionResult?token=2d2534b7daa64d67875a114a4944e140&userName=tjy123&deviceNumber=FF358B0BB697";
        $result = getCurl($url);
        //var_dump($result['data']);die;
        $value = $result['data'];  //不是二维数组用这个
        //foreach($result['data'] AS $key => $value){
            $value['createtime'] = date('Y-m-d H:i:s',time());
            $value['statisticstime'] = $value['newTime'];
            $value['statisticsDate'] = date('Y-m-d',strtotime($value['newTime']));
            $value['device_id'] = 27;//写死虫情设备
            $isRel = Db::name('bug_data')->where('newTime',$value['newTime'])->find();
            if (!empty($isRel)) {
                exit('重复记录不添加');
            }


            // 远程链接
            $remoteUrl = 'http://insect.sennor.net:1881/openFile/'.$value['downloadUrl'];
            $urlArr = explode('/',$remoteUrl);
            $imgName = end($urlArr);
            // 本地保存路径
            $localPath = "/www/wwwroot/test/public/uploads/".$imgName;
            // 使用file_get_contents函数获取远程文件的内容
            $content = file_get_contents($remoteUrl);
            // 使用file_put_contents函数将文件内容保存到本地
            file_put_contents($localPath, $content);


            $remoteUrl1 = 'http://insect.sennor.net:1881/openFile/'.$value['taggingImgUrl'];
            $urlArr1 = explode('/',$remoteUrl1);
            $imgName1 = end($urlArr1);
            // 本地保存路径
            $localPath1 = "/www/wwwroot/test/public/uploads/".$imgName1;
            // 使用file_get_contents函数获取远程文件的内容
            $content1 = file_get_contents($remoteUrl1);
            // 使用file_put_contents函数将文件内容保存到本地
            file_put_contents($localPath1, $content1);


            $value['downloadUrl'] = 'uploads/'.$imgName;
            $value['taggingImgUrl'] = 'uploads/'.$imgName1;

            Db::name('bug_data')->insert($value);
            if ($value['results'] == '未识别到对应虫类' || empty($value['results']) || $value['results'] == '数据提取成功') {
                exit('未识别到虫子不添加虫子统计');
            }
            $bugArr = explode(',',$value['results']);
            foreach ($bugArr AS $kk => $vv) {
                $typeArr = explode(':',$vv);
                $typeInfo = Db::name('bug_type')->where('name',$typeArr[0])->find();
                preg_match('/\d+/',$typeArr[1],$number);
                if (empty($typeInfo)) {
                    $typeId = Db::name('bug_type')->insertGetId(['name'=>$typeArr[0]]);
                    Db::name('bug_statistics')->insert(['type_id'=>$typeId,'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                } else {
                    $isStatistics = Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->find();
                    if (empty($isStatistics)) {
                        Db::name('bug_statistics')->insert(['type_id'=>$typeInfo['id'],'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                    } else {
                        Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->inc('num',$number[0])->update();
                    }
                }
            }
        //}
        exit('完成');
    }

    //获取虫情历史数据（暂时用不到）
    public function getHistoryData()
    {
        $url = "http://insect.sennor.net:1881/getAllInsectFileData?token=2d2534b7daa64d67875a114a4944e140&userName=tjy123&deviceNumber=FF353B6D234D&page=4&size=100";
        $result = getCurl($url);
        foreach($result['datas'] AS $key => $value){
            $value['createtime'] = date('Y-m-d H:i:s',time());
            $value['statisticstime'] = $value['newTime'];
            $value['statisticsDate'] = date('Y-m-d',strtotime($value['newTime']));
            Db::name('bug_data')->insert($value);
            if ($value['results'] == '未识别到对应虫类' || empty($value['results'])) {
                continue;
            }
            $bugArr = explode(',',$value['results']);
            foreach ($bugArr AS $kk => $vv) {
                $typeArr = explode(':',$vv);
                $typeInfo = Db::name('bug_type')->where('name',$typeArr[0])->find();
                preg_match('/\d+/',$typeArr[1],$number);
                if (empty($typeInfo)) {
                    $typeId = Db::name('bug_type')->insertGetId(['name'=>$typeArr[0]]);
                    Db::name('bug_statistics')->insert(['type_id'=>$typeId,'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                } else {
                    $isStatistics = Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->find();
                    if (empty($isStatistics)) {
                        Db::name('bug_statistics')->insert(['type_id'=>$typeInfo['id'],'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                    } else {
                        Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->inc('num',$number[0])->update();
                    }
                }
            }
        }
        exit('完成');
    }
}
